package jnpf.service.impl;


import cn.hutool.core.util.StrUtil;
import jnpf.common.enums.ragFlow.RagFlowApiEnum;
import jnpf.model.ragFlow.common.DocumentIdsReq;
import jnpf.model.ragFlow.common.IdReq;
import jnpf.model.ragFlow.common.RagFlowApiResult;
import jnpf.model.ragFlow.file.DocumentListRequest;
import jnpf.model.ragFlow.file.DocumentUpdateRequest;
import jnpf.model.ragFlow.file.FileListResp;
import jnpf.model.ragFlow.file.UpDocumentResp;
import jnpf.service.RagFlowFileService;
import jnpf.util.RagFlowApiReqUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static jnpf.util.ResultHandleUtil.txtConvertObj;


@Service
public class RagFlowFileServiceImpl implements RagFlowFileService {
    @Value("${ragflow.url:}")
    public String API_BASE_URL;
    @Value("${ragflow.knowApiKey}")
    public String API_KEY;
    @Autowired
    public UserProvider userProvider;

    @Override
    public RagFlowApiResult uploadFiles(String datasetId, List<MultipartFile> files) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        return txtConvertObj(RagFlowApiReqUtil.sendFileRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.UPLOAD_FILE, pathParams, files,userProvider.get().getUserId()), List.class);
    }

    @Override
    public RagFlowApiResult updateDocument(String datasetId, String documentId, DocumentUpdateRequest documentReq) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        pathParams.put("document_id", documentId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.UPDATE_DOCUMENT, pathParams, documentReq,userProvider.get().getUserId()), UpDocumentResp.class);
    }

    @Override
    public byte[] downloadDocument(String datasetId, String documentId) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        pathParams.put("document_id", documentId);
        // 假设 sendRequest 方法可以返回字节数组
        String s = RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.DOWNLOAD_DOCUMENT, pathParams, null, userProvider.get().getUserId());
        return s.getBytes();
    }

    @Override
    public RagFlowApiResult listDocuments(String datasetId, DocumentListRequest request) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        if (request.getPage() != null) {
            pathParams.put("page", request.getPage().toString());
        }
        if (request.getPageSize() != null) {
            pathParams.put("page_size", request.getPageSize().toString());
        }
        if (!StrUtil.isEmpty(request.getId())) {
            pathParams.put("id", request.getId().toString());
        }
        if (!StrUtil.isEmpty(request.getKeywords())) {
            pathParams.put("keywords", URLEncoder.encode(request.getKeywords().toString()));
        }
        if (!StrUtil.isEmpty(request.getOrderby())) {
            pathParams.put("orderby", request.getOrderby().toString());
        }
        if (request.getDesc() != null) {
            pathParams.put("desc", request.getDesc().toString());
        }
        if (!StrUtil.isEmpty(request.getId())) {
            pathParams.put("document_id", request.getId().toString());
        }
        if (!StrUtil.isEmpty(request.getName())) {
            pathParams.put("document_name", URLEncoder.encode(request.getName().toString()));
        }

        String s = RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.LIST_DOCUMENTS, pathParams, null, "admin");
        return txtConvertObj(s, FileListResp.class);
    }


    @Override
    public RagFlowApiResult deleteDocuments(String datasetId, IdReq documentIds) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.DELETE_DOCUMENTS, pathParams, documentIds,userProvider.get().getUserId()), UpDocumentResp.class);
    }

    @Override
    public RagFlowApiResult parseDocuments(String datasetId, DocumentIdsReq documentIds) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.PARSE_DOCUMENTS, pathParams, documentIds, userProvider.get().getUserId()),UpDocumentResp.class);
    }

    @Override
    public RagFlowApiResult stopParsingDocuments(String datasetId, DocumentIdsReq documentIds) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("dataset_id", datasetId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY, RagFlowApiEnum.STOP_PARSING_DOCUMENTS, pathParams, documentIds, userProvider.get().getUserId()),UpDocumentResp.class);
    }

}
